/* 
 * File:   graphcreator.h
 * Author: ben
 *
 * Created on March 29, 2009, 12:47 PM
 */

#ifndef _GRAPHCREATOR_H
#define	_GRAPHCREATOR_H

#include <opencv/highgui.h>
//#include <cv.h>
#include <opencv/cv.h>

#include "xmlParser.h"

#include "imgmanip.h"
#include "MyImg.h"

#define  FILE_OUT "relationships.xml"


using namespace std;

int AnalyseAll() {
    int i, j, n;
    MyImg test;
    ImgManip testing;
    double temp;

    ofstream fout1("results1.dat");
    ofstream fout2("results2.dat");
    ofstream foutlinks1("links1.dat");
    ofstream foutlinks2("links2.dat");
    ofstream foutnames("names1.dat");
    ostringstream ss1, ss2;

    for (i = 1; i < 13; i++) {

        ss1.str("");
        ss1 << "data/" << i << ".png";
        cout << ss1.str() << endl;

        for (j = 1; j < 13; j++) {
            ss2.str("");
            ss2 << "data/" << j << ".png";

            cout << ss2.str() << endl;
            testing.setFilenames(ss1.str(), ss2.str());
            temp = testing.Compare("FFT");
            cout << temp << endl;
            fout1 << temp << "\t";
            foutlinks1 << i << "\t" << j << "\t" << temp << endl;

            temp = testing.Compare("HIST");
            cout << temp << endl;
            fout2 << temp << "\t";
            foutlinks2 << i << "\t" << j << "\t" << temp << endl;
        }
        fout1 << endl;
        fout2 << endl;
        foutnames << i << "\t" << ss1.str() << endl;
    }
    fout1.close();
    fout2.close();
    foutlinks1.close();
    foutlinks2.close();
    foutnames.close();

    n = 2;
    ofstream foutw("weight.dat");
    for (i = 0; i < n; i++) {
        foutw << (double) 1 / n;
        if (i == n - 1) {

        } else {
            foutw << endl;
        }
    }
    foutw.close();
    return (0);
}

int AnalyseInXML(string folderpath, string fname) {
    int i, myIterator1 = 0, myIterator2 = 0, n, j,pos,k;
    string fname_full,tmp1,tmp2;
    string str_read1, str_read2;
    stringstream ss;
    ImgManip testing;
    string name_method,name_options;

    //INPUT
    fname_full = folderpath + "/" + fname;
    XMLNode xMainNode = XMLNode::openFileHelper(fname_full.data(), "list");
    XMLNode xNode = xMainNode.getChildNode("header");
    //OUTPUT
    XMLNode xMainNodeOut, xNodeOut;
    xMainNodeOut = XMLNode::createXMLTopNode("xml", TRUE);
    xMainNodeOut.addAttribute("version", "1.0");

    //cout << xNode.getChildNode("author").getAttribute("name") << endl;

    xNode = xMainNode.getChildNode("files");
    n = xNode.nChildNode("file");
    //cout << n << endl;
  
    xMainNodeOut.addChild("info");
    ss << n;
    xMainNodeOut.getChildNode("info").addAttribute("size",ss.str().data());
    ss.str("");
    ss << COMPARE_LAST;
    xMainNodeOut.getChildNode("info").addAttribute("nmethods",ss.str().data());
    xMainNodeOut.addChild("relationships");
    xNodeOut    =   xMainNodeOut.getChildNode("relationships");
  

        for (i = 0; i < n; i++) {
            myIterator2 = 0;
            tmp1        =   xNode.getChildNode("file", &myIterator1).getAttribute("path");
            str_read1 = folderpath + "/" + tmp1;

            
            for (j = 0; j < n; j++) {
                xNodeOut    =   xMainNodeOut.getChildNode("relationships");
                pos         =   n*i+j;
                tmp2        =   xNode.getChildNode("file", &myIterator2).getAttribute("path");
                str_read2   =   folderpath + "/" + tmp2;

                testing.setFilenames(str_read1,str_read2);

                xNodeOut.addChild("relation");//ajoute la relation au fichier xml
                xNodeOut.getChildNode("relation",pos).addAttribute("from",str_read1.data()); //ajoute from
                xNodeOut.getChildNode("relation",pos).addAttribute("to",str_read2.data()); //ajoute to
                for(k=0;k<COMPARE_LAST;k++){
                    ss.str("");
                    ss << testing.Compare(k);

                    testing.NameIt(k,&name_method,&name_options);
                    cout << str_read1.data() << "-->" << str_read2.data() << "\tmeth:" << name_method.data() << name_options.data() << endl;
                    if(xNodeOut.getChildNode("relation",pos).nChildNode(name_method.data())==0){
                        xNodeOut.getChildNode("relation",pos).addChild(name_method.data());
                    }
                    xNodeOut.getChildNode("relation",pos).getChildNode(name_method.data()).addChild(name_options.data());
                    xNodeOut.getChildNode("relation",pos).getChildNode(name_method.data()).getChildNode(name_options.data()).addAttribute("coeff",ss.str().data());
                }
                ss.str("");
                ss << 5;

                //cout << pos << endl;
                //cout << str_read1.data() << "--->" << str_read2.data() <<   endl;

            }

        }

    tmp2    =       folderpath+"/"+FILE_OUT;
    xMainNodeOut.writeToFile(tmp2.data(),"ISO-8859-1");
    //printf("    coeff %i=%s\n",i+1,xNode.getChildNode("NumericPredictor",&myIterator).getAttribute("coefficient"));
    return (0);
}



#endif	/* _GRAPHCREATOR_H */

